/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package grid;

import info.gridworld.grid.AbstractGrid;
import info.gridworld.grid.Location;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author Administrator
 */
public class HashSparseBoundedGrid <E> extends AbstractGrid<E>{

    public int rowLen;
    public int colLen;
    private Map<Location, E> occupantMap;

    public HashSparseBoundedGrid(int row, int col) {
        rowLen = row;
        colLen = col;
        occupantMap = new HashMap<Location, E>();
    }

    public int getNumRows() {
        return rowLen;
    }

    public int getNumCols() {
        return colLen;
    }

    public boolean isValid(Location loc) {
        return 0 <= loc.getRow() && loc.getRow() < getNumRows()
                && 0 <= loc.getCol() && loc.getCol() < getNumCols();
    }

    public E put(Location loc, E obj) {
        if (loc == null)
            throw new NullPointerException("loc == null");
        if (obj == null)
            throw new NullPointerException("obj == null");
        return occupantMap.put(loc, obj);
    }

    public E remove(Location loc) {
        if (loc == null)
            throw new NullPointerException("loc == null");
        return occupantMap.remove(loc);
    }

    public E get(Location loc) {
        if (loc == null)
            throw new NullPointerException("loc == null");
        return occupantMap.get(loc);
    }

    public ArrayList<Location> getOccupiedLocations() {
        ArrayList<Location> a = new ArrayList<Location>();
        for (Location loc : occupantMap.keySet())
            a.add(loc);
        return a;
    }

}
